package com.taobao.aft.format;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import com.taobao.aft.exception.AofTranslateException;
import com.taobao.aft.helper.Helper;
import com.taobao.inter.SplitInterface;

public class AofSelectCmdFormatBuilder extends CmdFormat {
	private final static String COMMAND = "SELECT";
	private final static String CMDARGC = "2";
	private final static String CMDNAMELEN = "6";
	
	private static AofSelectCmdFormatBuilder ab = null;
	
	
	private static SplitInterface sif = new SplitInterface() {
		
		@Override
		public String[] split(String data) {
			data = data.trim();
			return data.split("\\s+");
		}
	};
	
	
	@Override
	public CmdFormat createInstance() {
		if(ab == null) {
			ab = new AofSelectCmdFormatBuilder();
		}
		return ab;
	}
	
	public static void setSplitFunc(SplitInterface split) {
		sif = split;
	}
	
	private ByteBuffer getAofHeaderFormat(ByteBuffer byteBuffer)
			throws UnsupportedEncodingException {
		//*2\r\n
		byteBuffer.put(ARGCFLAG.getBytes(CHARSET));
		byteBuffer.put(CMDARGC.getBytes(CHARSET));
		byteBuffer.put(CLR.getBytes(CHARSET));
		
		//$6\r\n
		byteBuffer.put(CMDLENFLAG.getBytes(CHARSET));
		byteBuffer.put(CMDNAMELEN.getBytes(CHARSET));
		byteBuffer.put(CLR.getBytes());
		
		//SELECT\r\n
		byteBuffer.put(COMMAND.getBytes(CHARSET));
		byteBuffer.put(CLR.getBytes(CHARSET));

		return byteBuffer;
	}
	
	private ByteBuffer getAofKeyFormat(ByteBuffer byteBuffer, String index)
			throws UnsupportedEncodingException {
		
		byte[] byteIndex = index.getBytes(CHARSET);
		
		//$len\r\n
		byteBuffer.put(CMDLENFLAG.getBytes(CHARSET));
		byteBuffer.put(String.valueOf(byteIndex.length).getBytes(CHARSET));
		byteBuffer.put(CLR.getBytes(CHARSET));
		
		//dbid\r\n
		byteBuffer.put(byteIndex);
		byteBuffer.put(CLR.getBytes(CHARSET));
		
		return byteBuffer;
	}
	
	public byte[] getAofFormat(String index) throws AofTranslateException{
		String[] index_array = sif.split(index);
		if(index_array == null || index_array.length != 1) {
			throw new AofTranslateException();
		}
		index = index_array[0];
		if(index == null || "".equals(index) || Helper.isInteger(index) == false) {
			throw new AofTranslateException();
		}
		
		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
		byteBuffer.clear();
		try {
			byteBuffer = getAofHeaderFormat(byteBuffer);
			byteBuffer = getAofKeyFormat(byteBuffer, index);
		} catch(UnsupportedEncodingException e) {
			throw new AofTranslateException();
		}
		byte[] buffer = new byte[byteBuffer.position()];
		System.arraycopy(byteBuffer.array(), 0, buffer, 0, byteBuffer.position());
		return buffer;
	}
	
//	public static void main(String[] args) {
//		AofSelectCmdFormatBuilder afb = new AofSelectCmdFormatBuilder();
//		try {
//			String str = new String(afb.getAofFormat("0"));
//			System.out.println(str);
//		} catch (AofTranslateException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
